from datetime import datetime
from typing import Optional, List, Any
from pydantic import BaseModel, Field, field_validator

# 简化的ObjectId处理
def validate_object_id(v: Any) -> Optional[str]:
    """验证并转换ObjectId为字符串"""
    if v is None:
        return None
    if isinstance(v, str):
        return v
    # 如果是ObjectId对象，转换为字符串
    return str(v)

class ArticleModel(BaseModel):
    id: Optional[str] = Field(default=None, alias="_id")
    url: str = Field(..., description="文章原始URL")
    title: str = Field(..., description="文章标题")
    content: str = Field(..., description="文章正文内容")
    summary: Optional[str] = Field(None, description="文章摘要")
    author: Optional[str] = Field(None, description="作者")
    publish_date: Optional[datetime] = Field(None, description="发布日期")
    industry_keyword: str = Field(..., description="行业关键词")
    source_domain: str = Field(..., description="来源域名")
    crawl_time: datetime = Field(default_factory=datetime.now, description="爬取时间")
    content_length: int = Field(..., description="内容长度")
    language: Optional[str] = Field(None, description="内容语言")
    meta_description: Optional[str] = Field(None, description="页面描述")
    keywords: Optional[List[str]] = Field(None, description="页面关键词")
    status: str = Field(default="success", description="爬取状态")
    error_message: Optional[str] = Field(None, description="错误信息")

    class Config:
        populate_by_name = True
        arbitrary_types_allowed = True
    
    @field_validator('id', mode='before')
    @classmethod
    def validate_id(cls, v):
        return validate_object_id(v)

class CrawlResult(BaseModel):
    success: bool
    article: Optional[ArticleModel] = None
    error: Optional[str] = None
    url: str 